package cn.kgc.Adjudicative.shiro;

import cn.kgc.Adjudicative.entity.DUser;
import cn.kgc.Adjudicative.service.DUserService;
import cn.kgc.Adjudicative.token.JwtToken;
import cn.kgc.Adjudicative.utils.StringUtil;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * <p>Project:Adjudicative_document
 * <p>Package:cn.kgc.Adjudicative.shiro
 * <p>ClassName:MyCredentialsMatcher
 * <p>Powered by sun On 2023-01-09  09:56:56
 *
 * <p>Description:密码验证
 *
 * @author 孙杰
 * @version 1.0
 * @date 2023/1/9 9:56
 * @since 11
 */
@Component
public class MyCredentialsMatcher extends SimpleCredentialsMatcher {

    @Autowired
    private DUserService userService;

    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        JwtToken jwtToken = (JwtToken) token;
        if (jwtToken.getPassword() == null){
            return true;
        }
        String inPassword = new String(jwtToken.getPassword());
        String username = String.valueOf(info.getPrincipals());
        String dbPassword = (String) info.getCredentials();
        DUser user = userService.getUserByUsername(username);
        String salt = user.getSalt();
        return this.equals(StringUtil.md5(inPassword+salt),dbPassword);
    }
}
